<?php
namespace UserApi\Controller;
use Zend\Mvc\Controller\AbstractRestfulController;
use Zend\View\Model\JsonModel;
class ResultsApiController extends AbstractRestfulController
{
    public function getList()
    {			
		header('Access-Control-Allow-Origin: *');
		$questionsTable=$this->getServiceLocator()->get('Models\Model\QuestionsFactory');
		$questionsList = $questionsTable->getQuestionslist()->toarray();
		if(count($questionsList)!=0){
			return new JsonModel(array(
				'data' 	=> json_encode($questionsList),
				'value'	=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	1,
			));
		}	
    }
    public function get($user_id)
    {		
		header('Access-Control-Allow-Origin: *');
		$resultsTable=$this->getServiceLocator()->get('Models\Model\ResultsFactory');
		$getResults = $resultsTable->getResults($user_id)->toarray();
		if(count($getResults)!=0){
			return new JsonModel(array(
				'data' 	=> json_encode($getResults),
				'value'	=>	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'no data',
				'value'	=>	1,
			));
		}	
    }
    public function create($data)
    {
		header('Access-Control-Allow-Origin: *');	
		$resultsTable=$this->getServiceLocator()->get('Models\Model\ResultsFactory');
		$saveResults = $resultsTable->saveResults($data);
		if($saveResults!=0){
			return new JsonModel(array(
				'output' 	=> 	'success',
				'ResultsId'	=>	$saveResults,
				'value' 	=> 	1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function update($uid, $data)
    {
		header('Access-Control-Allow-Origin: *');	
		$userTable=$this->getServiceLocator()->get('Models\Model\UserFactory');
		$userDetailsTable=$this->getServiceLocator()->get('Models\Model\UserDetailsFactory');
		$userRegistration = $userTable->updateUserData($data,$uid);
		$userRegistrationDetails = $userDetailsTable->updateUserDetails($data,$uid);
		if($userRegistrationDetails){
			return new JsonModel(array(
				'output' => 'success',
				'value' => 1,
			));
		}else{
			return new JsonModel(array(
				'output' => 'fail',
				'value' => 0,
			));
		}
    }
    public function delete($id)
    {
       
    }
}